<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active: active }">
      <span v-if="!changeResult.resultAddress" class="placeholder"
        >请选择配送地址</span
      >
      <span v-else class="value">{{ changeResult.resultAddress }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span
        @click="changeSelect(item)"
        class="ellipsis"
        v-for="item in dataList"
        :key="item.code"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<script>
import { ref, reactive, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  // 点击交互
  // 1. 每一个单项中都有一个名称为areaList的字段存放着当前下面的子级列表数据
  // 2. 每一个单项中都有一个名称为level字段 代表当前是省->0  市->1 区->2

  // 思路: 负责当前区域的模板渲染的数据是 dataList 点击的时候如果想要修改这块区域的显示
  // 必定要把 dataList 变成一个全新的list 这个全新的list其实就是点击这一项数据下面的 areaList
  setup (props, { emit }) {
    // 控制展开收起,默认收起
    const active = ref(false)
    // 打开弹框
    function open () {
      active.value = true
    }
    // 关闭弹框
    function close () {
      active.value = false
    }
    // 切换展开收起
    function toggleDialog () {
      if (active.value) {
        close()
      } else {
        open()
      }
    }

    // 获取数据
    const dataList = ref(null)
    let cacheCityJSON = ''
    async function loadCityList () {
      const res = await axios.get(
        'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      )

      dataList.value = res.data
      cacheCityJSON = JSON.stringify(res.data)
    }

    loadCityList()

    // 操作
    // 3. 交互选择
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '', // 地区名
      resultAddress: '' // 最终结果地址
    })

    const changeSelect = item => {
      if (item.level === 0) {
        changeResult.provinceCode = item.code
        changeResult.provinceName = item.name
      } else if (item.level === 1) {
        changeResult.cityCode = item.code
        changeResult.cityName = item.name
      } else if (item.level === 2) {
        changeResult.countyCode = item.code
        changeResult.countyName = item.name
        changeResult.resultAddress = `${changeResult.provinceName} > ${changeResult.cityName} > ${changeResult.countyName}`
        changeResult.fullLocation = `${changeResult.provinceName}${changeResult.cityName}${changeResult.countyName}`

        close()

        emit('change', changeResult)
      }

      dataList.value = item.areaList
    }

    // 监听active
    watch(active, newValue => {
      if (newValue) {
        dataList.value = JSON.parse(cacheCityJSON)
      }
    })

    return { active, toggleDialog, dataList, changeSelect, changeResult }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
